package com.mass.waybill.voucher.service.impl;

import com.mass.core.utils.AjaxResponse;
import com.mass.core.utils.DateUtils;
import com.mass.waybill.voucher.dao.WbVoucherMapper;
import com.mass.waybill.voucher.model.WbVoucher;
import com.mass.waybill.voucher.model.WbVoucherExample;
import com.mass.waybill.voucher.service.WbVoucherService;
import freemarker.template.utility.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @Author: ZSX
 * @Description: 收款单 service 接口  实现类
 */
@Service
@Transactional
public class WbVoucherServiceImpl implements WbVoucherService {

    @Autowired
    private WbVoucherMapper wbVoucherMapper;

    /**
     * 根据订单编号查询订单明细
     *
     * @param orderCode
     * @return
     */
    @Override
    public List<WbVoucher> findByOrderCode(String orderCode) {
        return wbVoucherMapper.findByOrderCode(orderCode);
    }

    @Transactional
    @Override
    public int insert(WbVoucher wbVoucher) {
        wbVoucher.setDelState(0);
        wbVoucher.setCreateTime(DateUtils.getDate());
        wbVoucher.setPaymentTime(DateUtils.getDate("yyyy年MM月dd日 HH:mm"));
        int number = wbVoucherMapper.insert(wbVoucher);
        if (number > 0) {
            wbVoucher.setSettlementNumber(DateUtils.getDate("yyyyMMdd") + StringUtil.leftPad(wbVoucher.getId().toString(), 4, "0"));
            wbVoucherMapper.updateByPrimaryKeySelective(wbVoucher);
        }
        return number;
    }

    @Transactional
    @Override
    public int delete(Long wbVoucherId) {
        WbVoucher wbVoucher = wbVoucherMapper.selectByPrimaryKey(wbVoucherId);
        wbVoucher.setDelState(1);//删除状态
        return wbVoucherMapper.updateByPrimaryKeySelective(wbVoucher);
    }

    @Override
    public int deleteByIds(List<Long> ids) {
        WbVoucher wbVoucher = new WbVoucher();
        wbVoucher.setDelState(1);//删除状态
        WbVoucherExample example = new WbVoucherExample();
        WbVoucherExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);
        return wbVoucherMapper.updateByExampleSelective(wbVoucher,example);
    }

    @Transactional
    @Override
    public int update(WbVoucher wbVoucher) {
        return wbVoucherMapper.updateByPrimaryKeySelective(wbVoucher);
    }

    @Override
    public WbVoucher selectById(Long wbVoucherId) {
        return wbVoucherMapper.selectByPrimaryKey(wbVoucherId);
    }

    @Override
    public AjaxResponse selectPage(WbVoucher wbVoucher, Integer page, Integer limit) {
        WbVoucherExample example = new WbVoucherExample();
        if (!ObjectUtils.isEmpty(page)) {
            example.setPageIndex((page - 1) * limit);
            example.setPageSize(limit);
        }
        example.setOrderByClause(" date_receipt  desc ");
        WbVoucherExample.Criteria criteria = example.createCriteria();
        criteria.andDelStateEqualTo(0);//获取正常数据
        if (!ObjectUtils.isEmpty(wbVoucher)) {
            //付款日期范围
            if (!ObjectUtils.isEmpty(wbVoucher.getDateReceipt())) {
                criteria.andDateReceiptBetween(wbVoucher.getDateReceipt().split("~")[0].trim(), wbVoucher.getDateReceipt().split("~")[1].trim());
            }
            //模糊查询 结算编号
            if (!ObjectUtils.isEmpty(wbVoucher.getSettlementNumber())) {
                criteria.andSettlementNumberLike("%" + wbVoucher.getSettlementNumber().trim() + "%");
            }
            //账户查询
            if (!ObjectUtils.isEmpty(wbVoucher.getPaymentAccount())) {
                criteria.andPaymentAccountEqualTo(wbVoucher.getPaymentAccount().trim());
            }
            //模糊查询 付款人
            if (!ObjectUtils.isEmpty(wbVoucher.getDrawee())) {
                criteria.andDraweeLike("%" + wbVoucher.getDrawee().trim() + "%");
            }
            //付款类型
            if (!ObjectUtils.isEmpty(wbVoucher.getTypePayer())) {
                criteria.andTypePayerEqualTo(wbVoucher.getTypePayer());
            }
            //付款单位
            if (!ObjectUtils.isEmpty(wbVoucher.getPaymentUnit())) {
                criteria.andPaymentUnitEqualTo(wbVoucher.getPaymentUnit().trim());
            }

            // amountCancelVerification  为 1 时 查询未核销数据
            if(!ObjectUtils.isEmpty(wbVoucher.getAmountCancelVerification())){
                //未核销
                if("1".equals(wbVoucher.getAmountCancelVerification().trim())){
                    criteria.andAmountCancelVerificationIsNull();
                //核销完成
                }else if("2".equals(wbVoucher.getAmountCancelVerification().trim())){
                    criteria.andAmountCancelVerificationIsNotNull();
                }

            }
        }
        List<WbVoucher> list = wbVoucherMapper.selectByExample(example);
        Long count = wbVoucherMapper.countByExample(example);
        return AjaxResponse.success("ok", list, count, page);
    }
}
